<template>
  <!-- 销售订单汇总统计表   -->
  <div class="app-container" v-loading="loading">
    <el-form :model="queryParams" ref="queryForm" :inline="true" class="search-form-new" label-width="80px">

      <el-form-item el-form-item label="店铺" prop="status" v-hasPermi="['searchTenantID']">
        <el-select v-model="queryParams.tenantId" placeholder="请选择店铺" clearable filterable>
          <el-option v-for="dict in tenantList" :key="dict.tenantId" :label="dict.tenantName" :value="dict.tenantId" />
        </el-select>
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择订单类型" clearable filterable>
          <el-option v-for="(item,key) in commonMaps.orderTypeMap" :key="key" :label="item.label" :value="key" />
        </el-select>
      </el-form-item>
      <el-form-item label="买家昵称" prop="saleChannel">
        <el-input v-model="queryParams.saleChannel" placeholder="请输入买家昵称" clearable />
      </el-form-item>
      <el-form-item label="供货商" prop="supplyChannel">
        <el-input v-model="queryParams.supplyChannel" placeholder="请输入供货商" clearable />
      </el-form-item>
      <el-form-item label="订单号" prop="no">
        <el-input v-model="queryParams.no" placeholder="请输入订单号" clearable @change="searchList" />
      </el-form-item>
      <el-form-item label="商品编号" prop="goodsId">
        <el-input v-model="queryParams.goodsId" placeholder="请输入商品编号" clearable @change="searchList" />
      </el-form-item>
      <el-form-item label="游戏账号" prop="accountNumber">
        <el-input v-model="queryParams.accountNumber" placeholder="请输入游戏账号" clearable />
      </el-form-item>
      <el-form-item label="支付状态" prop="">
        <el-select v-model="queryParams.salePay" placeholder="请选择支付状态" clearable>
          <el-option v-for="(dict, key) in commonMaps.buyerPayState" :key="key" :label="dict" :value="key" />
        </el-select>
      </el-form-item>

      <el-form-item label="销售人" prop="saleUserId">
        <virtual-select v-model="queryParams.saleUserId" :list="allUsers" label="nickName" value="userId" :placeholder="'输入关键字用于搜索用户'" :keeps-params="20" :is-concat="false" :concat-symbol="' || '" :is-multiple="false" :disabled="false" :size="'small'" :allow-create="false" />
      </el-form-item>
      <el-form-item label="下单日期" prop="remark">
        <el-date-picker v-model="timer" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd" @change="searchList" size="small">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="完成日期" prop="remark">
        <el-date-picker v-model="timer2" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd" @change="searchList" size="small">
        </el-date-picker>
      </el-form-item>

      <el-form-item class="btns">
        <el-button type="primary" icon="el-icon-search" @click="searchList">搜索</el-button>
        <el-button icon="el-icon-refresh" type="warning" @click="resetQuery">重置</el-button>
        <el-button icon="el-icon-download" type="warning" plain @click="exportOrder">导出</el-button>

      </el-form-item>
    </el-form>
    <div class="huizong">
      <el-descriptions :label-style="{fontWeight:600,color:'#333'}" border direction="vertical" :column="7" title="订单总额">
        <el-descriptions-item label="买家实付">{{totalData?totalData.actualAmountSum:0}}</el-descriptions-item>
        <el-descriptions-item label="订单利润">{{totalData?totalData.profitsSum:0}}</el-descriptions-item>
        <el-descriptions-item label="售后赔付">{{totalData?totalData.platformPayAmountSum:0}}</el-descriptions-item>
        <el-descriptions-item label="真实利润">
          <span class="price">{{totalData?(Math.round(((totalData.profitsSum||0) - (totalData.platformPayAmountSum||0))*100)/100):0}}</span>
        </el-descriptions-item>
      </el-descriptions>
    </div>
    <div class="table_box">
      <el-table :data="tableData.records" style="width: 100%" stripe>

        <el-table-column prop="" label="订单来源" min-width="120">
          <template slot-scope="scope">
            <div>
              {{scope.row.sourceScen?commonMaps.orderSource[scope.row.sourceScen].label:'无'}}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="" label="订单类型" width="100">
          <template slot-scope="scope">
            <div>
              <el-tag :type="commonMaps.orderTypeMap[scope.row.orderType].type">{{scope.row.orderType?commonMaps.orderTypeMap[scope.row.orderType].label:'无'}}</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="no" label="订单编号" min-width="200">
          <template slot-scope="scope">
            <span class="link" @click="goOrderDetail(scope.row.id)">{{
                scope.row.no
                }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="goodsId" label="商品编号" min-width="100">

        </el-table-column>
        <el-table-column prop="accountNumber" label="游戏账号" min-width="100"> </el-table-column>
        <el-table-column prop="saleChannel" label="买家昵称" min-width="100"></el-table-column>
        <el-table-column prop="supplyChannel" label="供货商" min-width="100"></el-table-column>
        <el-table-column prop="saleUserNickname" label="销售人" min-width="100"></el-table-column>
        <el-table-column prop="actualAmount" label="买家实付" min-width="100"></el-table-column>
        <el-table-column prop="" label="支付状态" min-width="120">
          <template slot-scope="scope">
            <span>{{
                  commonMaps.buyerPayState[scope.row.salePay] || commonMaps. buyerPayState["1"]
                }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="profits" label="利润" min-width="100">
          <template slot-scope="scope">
            <div>
              <span :style="{color:(scope.row.platformPayAmount?  scope.row.profits - scope.row.platformPayAmount:scope.row.profits)>0?'#027444':'#ff585f'}">{{ scope.row.platformPayAmount?  scope.row.profits - scope.row.platformPayAmount:scope.row.profits}}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" min-width="120"></el-table-column>
        <el-table-column prop="createTime" label="下单时间" min-width="160"></el-table-column>
        <el-table-column prop="finishTime" label="完成时间" min-width="160"></el-table-column>
        <el-table-column prop="afterNo" label="售后单号" min-width="120">
          <template slot-scope="scope">
            <span class="link" @click="goAfterOrderDetail(scope.row.afterId)">{{
                scope.row.afterNo
                }}</span>
          </template>

        </el-table-column>
        <el-table-column prop="platformPayAmount" label="售后赔付" min-width="100"></el-table-column>
        <el-table-column prop="" label="售后打款状态" min-width="130">
          <template slot-scope="scope">
            <span>{{
                  commonMaps.afterOrderPayStateTenant[scope.row.remitState] || commonMaps.afterOrderPayStateTenant["1"]
                }}</span><br />

          </template>
        </el-table-column>

      </el-table>
      <pagination v-show="tableData.total > 0" :total="tableData.total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    </div>

    <!-- <h5Dialog ref="h5Dialog"></h5Dialog> -->
    <orderDetailDialog ref="orderDetailDialog"></orderDetailDialog>
    <afterSellView ref="afterSellView"></afterSellView>
  </div>
</template>

<script>
import orderDetailDialog from "./components/orderDetailDialog";
import orderApi from "@/api/pickUp/order.js";
import { getDicts } from "@/api/system/dict/data";
import supplyConfigApi from "@/api/system/supplyConfig";
import VirtualSelect from "@/components/VirtualSelect.vue";
import afterSellView from "../saleAfterOrder/components/afterSellView.vue";
export default {
  name: "orderStatisicsList",
  components: {
    orderDetailDialog,
    VirtualSelect,
    afterSellView,
  },
  data() {
    return {
      totalData: {},
      timer: [],
      timer2: [],
      toGroups: "", //历史消息群id
      // 提号时间，商品ID 游戏类型 区服 实名状态   游戏账号【对应的商品链接】
      tableData: {
        records: [],
        total: 0,
      },
      allUsers: [], //所有用户列表
      btnLoading: false,
      loading: false,
      actived: "3",
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        account: null,
        accountId: null,
        tel: null,
      },
    };
  },
  computed: {
    tenantList() {
      return this.$store.getters.tenantList;
    },
  },
  created() {
    this.getList();
    this.getAllUserList();
  },
  activated() {},
  methods: {
    // 获取所有用户
    getAllUserList(val) {
      supplyConfigApi.getAllUserList().then((res) => {
        this.allUsers = res.rows;
      });
    },

  
    // 获取列表
    getList() {
      // queryParams 请求列表
      this.loading = true;
      if (this.timer && this.timer.length == 2) {
        this.queryParams.createTimeStart = this.timer[0] + " 00:00:00";
        this.queryParams.createTimeEnd = this.timer[1] + " 23:59:59";
      } else {
        this.queryParams.createTimeStart = "";
        this.queryParams.createTimeEnd = "";
      }
      if (this.timer2 && this.timer2.length == 2) {
        this.queryParams.finishTimeStart = this.timer2[0] + " 00:00:00";
        this.queryParams.finishTimeEnd = this.timer2[1] + " 23:59:59";
      } else {
        this.queryParams.finishTimeStart = "";
        this.queryParams.finishTimeEnd = "";
      }
      orderApi.saleOrderStatisticsList(this.queryParams).then((res) => {
        this.loading = false;
        this.tableData.records = res.rows;
        this.totalData = res.params;
        this.tableData.total = res.total;
      });
    },
    // 搜索
    searchList() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    //查看订单详情
    goOrderDetail(id) {
      this.$refs.orderDetailDialog.open(id, "kefu");
    },
    // 查看售后详情
    goAfterOrderDetail(id) {
      this.$refs.afterSellView.open(id);
    },

    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        account: null,
        accountId: null,
        tel: null,
      };
      this.timer = [];
      this.timer2 = [];
      this.getList();
    },
    // 每页条数改变
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getList();
    },
    // 页码改变
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getList();
    },
    exportOrder() {
      const obj = {
        ...this.queryParams,
        pageNum:null,
        pageSize:null
      }
      this.loading = true;
      orderApi.saleOrderexport(obj).then(res=>{
        this.loading = false
        // this.$download.name(res.msg);
        this.$download.downloadFile(res,'销售订单统计');
      })
    },
  },
};
</script>
<style lang="scss" scoped>
.huizong {
  padding: 15px;
  margin-bottom: 15px;
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.2);
}
.price {
  font-size: 16px;
  color: crimson;
  font-weight: 600;
}
.link {
  color: blue;
  cursor: pointer;
}
</style>
